WARNING: This program has a logic error. The cow is hungry and wants to eat the hay, but nothing happens!
RULE: An event handler links a sprite to a function that is called whenever the event is triggered.
Click Run and see what happens. Look in the code to see where the cow should be eating the hay.
Fix the program by adding an event handler to the main() function so that the collision event happens.
Click Run to test if you fixed the program. When it is fixed, click Submit and Next.
To navigate the page using the TAB key, first press ESC to exit the code editor.
def add_sprite():
""" Add cow sprite to the stage and set its speed """
cow = codesters.Sprite("cow",-400, -150)
cow.set_x_speed(3)
return cow
def setup_stage():
""" Set up the stage with background and sprites """
stage.set_background("barn")
stage.disable_right_wall()
hay = codesters.Sprite("hay",-150,-150)
hay = codesters.Sprite("hay",0,-150)
hay = codesters.Sprite("hay",150,-150)
def collision(sprite, hit_sprite):
""" Collision event for the cow to eat hay """
if hit_sprite.get_image_name() == "hay":
sprite.say("yum!",.2)
hit_sprite.hide()
def main():
""" Sets up the program and calls other functions """
setup_stage()
sprite = add_sprite()
main()
t = codesters.Teacher()
defs = t.find_block("def")
collision_event_handlers = t.find_text("event_collision")
def get_above_def(line_number, def_list):
""" Returns the name of the nearest function and the distance above the given line number """
function_name = ""
distance = line_number - def_list[0][0]
for func in def_list:
if line_number - func[0] <= distance and line_number - func[0] > 0:
function_name = func[1]
distance = line_number - func[0]
return function_name, distance
def find_new_function(ignore_list, def_list):
""" Returns tuples of (line_number, function def) for any functions that aren't specified in ignore list """
return_list = []
for d in def_list:
count = 0
for i in ignore_list:
if i in d[1]:
break
else:
count += 1
if count == len(ignore_list):
return_list.append((d[0],d[1]))
return return_list
try:
tval2 = collision_event_handlers[0][1]
tval2_line_num = collision_event_handlers[0][0]
tval2_indent = t.get_indent_at_line(tval2_line_num)
except:
tval2 = "DNE"
tval2_line_num = "DNE"
tval2_indent = "DNE"
try:
above_def, distance = get_above_def(tval2_line_num, defs)
except:
above_def = "DNE"
distance = "DNE"
t1 = TestObjective()
t1.add_success("sprite." in tval2 and "(collision)" in tval2, "Great job!")
t1.add_failure(tval2 == "DNE", "Oops, did you add a Collision Event Handler to the main() function?")
t1.add_failure("sprite." not in tval2, "Oops, make sure the event handler is connecting to sprite!")
t1.add_failure("(collision)" not in tval2, "Oops, make sure the function in the event handler is collision?")
t2 = TestObjective()
t2.add_success("main" in above_def and tval2_indent == 4, "Great job!")
t2.add_failure("main" not in above_def, "Did you place Collision Event Handler within main()?")
t2.add_failure(tval2_indent < 0 or tval2_indent > 4, "Make sure that the event handler is indented once within main()!")
tester = TestManager()
tester.add_test_list([t1, t2])
tester.run_tests()
tester.display_first_feedback()
Are you already running a Codesters project in another tab or window?
Micro:bit can only connect to one web page at a time.
Try stopping other Codesters projects or closing
other tabs or windows that may be using your Micro:bit.
If that doesn't fix the problem try disconnecting your Micro:bit,
reloading this page, and reconnecting your Micro:bit.